package com.df.hitime.common;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;

/**
 * @Description: xml工具类
 * @Author: huangwl
 * @CreateDate: 2018/11/28 9:52
 */
public class XmlUtils {

    public static String objectToXml(Object object){
        XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
        //设置别名, 默认会输出全路径
        xStream.alias("xml", Object.class);
        //转为xml
        String xml = xStream.toXML(object);
        return xml;
    }

    public static Object xmlToObject(String xml,Class cls){
        XStream xStream1= new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
        xStream1.alias("xml", cls);
        Object object= (Object) xStream1.fromXML(xml.toString());
        return object;
    }
}
